package service

import (
	"gorm.io/gorm"
	"ruoyi-go/app/admin/model/system"
	"ruoyi-go/app/admin/model/tools"
	"ruoyi-go/pkg/mysql"
)

// SysWechatService 提供外部调用对象
var SysWechatService = &sysWechatService{}

// 数据库操作对象
var db *gorm.DB

type sysWechatService struct{}

func (service sysWechatService) Save(wechat system.SysWechat) {
	err := mysql.MysqlDb().Create(&wechat).Error
	if err != nil {
		panic(err.Error())
	}
}

func (service sysWechatService) Edit(wechat system.SysWechat) {
	err := mysql.MysqlDb().Save(&wechat).Error
	if err != nil {
		panic(err.Error())
	}
}

func (service sysWechatService) Delete(id int64) {
	err := mysql.MysqlDb().Delete(&system.SysWechat{}, id).Error
	if err != nil {
		panic(err.Error())
	}
}

func (service sysWechatService) Detail(id int64) system.SysWechat {
	var wechat system.SysWechat
	err := mysql.MysqlDb().First(&wechat, id).Error
	if err != nil {
		panic(err.Error())
	}
	return wechat

}

func (service sysWechatService) Page(qo system.SysWechatQO) tools.TableDataInfo {
	var total int64
	var rows []system.SysWechat
	tx := mysql.MysqlDb().Model(&system.SysWechat{}).Limit(qo.PageSize).Offset((qo.PageNum - 1) * qo.PageSize).Order("create_time desc")
	if qo.AppId != "" {
		tx.Where("app_id like concat('%',?,'%')", qo.AppId)
	}
	if qo.AppSecret != "" {
		tx.Where("app_secret like concat('%',?,'%')", qo.AppSecret)
	}
	if qo.Remark != "" {
		tx.Where("remark like concat('%',?,'%')", qo.Remark)
	}
	tx.Find(&rows)
	tx.Count(&total)
	return tools.TableDataInfo{Rows: rows, Total: total}
}
